const { ccclass, property } = cc._decorator;

@ccclass
export default class levelFindSpriteItemBase extends cc.Component {

    @property({ type: cc.Node, displayName: "mask节点" })
    maskNode: cc.Node = null;
    @property({ displayName: "是否有滑动组件" })
    hasScorll: boolean = false;
    @property({ displayName: "是否阻挡滑动", visible() { return this.hasScorll } })
    isStopScorll: boolean = true;
    @property({ type: cc.ScrollView, displayName: "滑动组件", visible() { return this.hasScorll } })
    scorllNode: cc.ScrollView = null;


    protected onLoad(): void {
        this.node.on('touchstart', this.onTouchStart, this);
        this.node.on('touchmove', this.onTouchMove, this);
        this.node.on('touchend', this.onTouchEnd, this);
        this.node.on('touchcancel', this.onTouchCancel, this);
        this.node.on(cc.Node.EventType.MOUSE_WHEEL, this.onMapMouseWheeL.bind(this), this)
    }

    onMapMouseWheeL(event: cc.Event.EventMouse) {
        // return
        //点击node的坐标
        let pos = this.node.convertToNodeSpaceAR(cc.v2(event.getLocationX(), event.getLocationY()));
        //滑轮缩放大小  
        let scX = (this.node.scaleX - (event.getScrollY() / 10000 * -1));
        //当前缩放值与原来缩放值之差
        let disScale = scX - this.node.scaleX;
        //当前点击的坐标与缩放值差像乘 
        let gapPos = pos.scale(cc.v2(disScale, disScale));
        //当前node坐标位置减去点击 点击坐标和缩放值的值
        let mapPos = this.node.getPosition().sub(gapPos);
        // console.log("scX",scX)
        if (this.isMinScale(scX)) return;
        if (this.isMaxScale(scX)) return;
        //设放大比列  
        this.node.setScale(scX, scX)

        // NewLBXKuai.node.setScale(scX,scX)
        //移动地图坐标
        this.node.setPosition(mapPos)
        this.fixPos(cc.v2(0, 0))
    }
    sensitivityNum: number = 0.5;

    oldScale: number = 0.3;

    touchOne: cc.Touch = null;
    touchTwo: cc.Touch = null;

    @property({ displayName: "最大缩放倍数" })
    maxScale: number = 5;

    @property({ displayName: "最小缩放倍数" })
    minScale: number = 1;
    @property({ type: cc.Float })
    moveMul: number = 1;

    @property({ type: cc.Float })
    scaleMul: number = 1;
    openScroll(open: boolean) {
        if (this.hasScorll && this.isStopScorll && this.scorllNode) {
            // this.scorllNode.horizontal = open;
            this.scorllNode.enabled = open;
        }
    }
    onTouchStart(e: cc.Event.EventTouch) {
        this.openScroll(false)
        if (!this.touchOne) this.touchOne = e.touch;
        if (!this.touchTwo) this.touchTwo = e.touch;
    }

    onTouchMove(e: cc.Event.EventTouch) {
        this.openScroll(false)
        e.stopPropagation();
        // if (Normallevel134._instance.isPlaySound) return;
        let touches = e.getTouches();
        let touchPos: cc.Vec2 = e.getLocation();

        // if(this.touchOne){
        //     console.log(this.touchOne.getDelta(),"移动的距离");
        // }

        if (touches.length == 1) {

            // let maxWidth:number = 

            // let scale = this.node.scale == 1?1.1:this.node.scale; 
            // if (this.node.scale > 1) {
            //     // var touches3 = touchEvent.getTouches();
            //     // var w_pos = touches[0].getLocation();
            //     var delta3 = touches[0].getDelta();
            //     var widthMax = 375;
            //     var hightMax = 520;
            //     if (this.node.position.x >= (widthMax * (this.node.scale - 1)) && delta3.x > 0) {
            //         delta3.x = 0;
            //     }
            //     else if (this.node.position.x <= -(widthMax * (this.node.scale - 1)) && delta3.x < 0) {
            //         delta3.x = 0;
            //     }
            //     if (this.node.position.y >= (hightMax * (this.node.scale - 1)) && delta3.y > 0) {
            //         delta3.y = 0;
            //     }
            //     else if (this.node.position.y <= -(hightMax * (this.node.scale - 1)) && delta3.y < 0) {
            //         delta3.y = 0;
            //     }
            //     let newPos = delta3.add(new cc.Vec2(this.node.position.x, this.node.position.y));
            //     this.node.setPosition(newPos);
            // }
            // else if (this.node.scale == 1) {
            // var delta3 = touches[0].getDelta();
            // var widthMax = 375;
            // var hightMax = 163;
            // if (this.node.position.x >= widthMax  && delta3.x > 0) {
            //     delta3.x = 0;
            // }
            // else if (this.node.position.x <= -widthMax && delta3.x < 0) {
            //     delta3.x = 0;
            // }
            // if (this.node.position.y >= hightMax && delta3.y > 0) {
            //     delta3.y = 0;
            // }
            // else if (this.node.position.y <= -hightMax && delta3.y < 0) {
            //     delta3.y = 0;
            // }
            // let newPos = delta3.add(new cc.Vec2(this.node.position.x, this.node.position.y));
            // this.node.setPosition(newPos);
            // }


            // let xMin:number = this.node.x+(-this.node.width/2*this.node.scale);
            // let xMax:number = this.node.x+(this.node.width/2*this.node.scale);
            // let yMin:number = this.node.y+(-this.node.height/2*this.node.scale);
            // let yMax:number = this.node.y+(this.node.height/2*this.node.scale);

            // console.log(xMin,xMax,yMin,yMax,minX,maxX,minY,maxY,"???????怎么回事");

            // if(xMin <= minX || xMax >= maxX){

            // }else{
            //     this.node.x += detal.x * this.node.scale;
            // }

            // if(yMin <= minY || yMax <= maxY){

            // }else{

            //     this.node.y += detal.y * this.node.scale;
            // }

            // if(this.node.y - (this.node.height/2* this.node.scale)<= minY){
            //     this.node.y = minY;
            // }
            // if(this.node.y + (this.node.height/2* this.node.scale)>= maxY){
            //     this.node.y = maxY;
            // }

            let maxXDistance: number = ((this.node.width * this.node.scale) - this.node.parent.width) / 2;
            let maxYDistance: number = ((this.node.height * this.node.scale) - this.node.parent.height) / 2;
            let delta = e.getDelta().mulSelf(this.moveMul);

            let dx = delta.x;
            let dy = delta.y;

            // this.node.x += delta.x //* this.node.scale;
            // this.node.y += delta.y //* this.node.scale; 

            if (this.node.x + dx < -maxXDistance) {
                // this.node.x = -maxXDistance;
                dx = 0;
            }
            if (this.node.x + dx > maxXDistance) {
                // this.node.x = maxXDistance;
                dx = 0;
            }

            if (this.node.y + dy < -maxYDistance) {
                // this.node.y = -maxYDistance;
                dy = 0;
            }
            if (this.node.y + dy > maxYDistance) {
                // this.node.y = maxYDistance;
                dy = 0;
            }

            this.node.x += dx;
            this.node.y += dy;
        }

        if (touches.length == 2) {
            let touch1 = touches[0], touch2 = touches[1];
            let delta1 = touch1.getDelta();
            let delta2 = touch2.getDelta();
            let touchPoint1 = this.node.convertToNodeSpaceAR(touch1.getLocation())
            let touchPoint2 = this.node.convertToNodeSpaceAR(touch2.getLocation())
            let distance = touchPoint1.sub(touchPoint2);
            let delta = delta1.sub(delta2).mulSelf(this.scaleMul);
            let scale = 1;
            if (Math.abs(distance.x) > Math.abs(distance.y)) {
                scale = (distance.x + delta.x) / distance.x * this.node.scaleX;
            }
            else {
                scale = (distance.y + delta.y) / distance.y * this.node.scaleY;
            }

            let pos = touchPoint2.add(cc.v2(distance.x / 2, distance.y / 2))
            //console.log("pos:",Math.floor(pos.x),Math.floor(pos.y));
            //滑轮缩放大小
            let scX = scale;
            //当前缩放值与原来缩放值之差
            let disScale = scX - this.node.scaleX;
            //当前点击的坐标与缩放值差像乘 
            let gapPos = pos.scale(cc.v2(disScale, disScale));
            //当前node坐标位置减去点击 点击坐标和缩放值的值
            let mapPos = this.node.getPosition().sub(gapPos);
            //console.log("mapPos:",Math.floor(mapPos.x),Math.floor(mapPos.y));

            //放大缩小
            if (this.isMinScale(scale)) return;
            if (this.isMaxScale(scale)) return;
            this.node.setScale(scale, scale)

            // let pre3 = cc.instantiate(this.redPre)
            // this.pre3 = pre3;
            // pre3.setPosition(pos)
            // pre3.scaleX = 2
            // pre3.scaleY = 2
            // this.node.addChild(pre3);
            //移动地图坐标
            this.node.setPosition(mapPos)
            this.fixPos(cc.v2(0, 0))
        }
    }

    onTouchEnd(e: cc.Event.EventTouch) {
        this.openScroll(true)
        // console.log("松开节点????????????");
        // if (this.node.scale <= 1) {
        //     this.node.scale = 1;
        //     this.node.setPosition(cc.Vec3.ZERO);
        // }
    }

    onTouchCancel(e: cc.Event.EventTouch) {
        this.openScroll(true)
        // console.log("离开节点?????????");
        // if (this.node.scale <= 1) {
        //     this.node.scale = 1;
        //     this.node.setPosition(cc.Vec3.ZERO);
        // }
    }

    //修正坐标
    fixPos(delta) {
        let x = this.node.x + delta.x;
        let y = this.node.y + delta.y;
        let cW = this.node.width * this.node.scaleX
        let cH = this.node.height * this.node.scaleY

        let winSize = cc.size(this.node.parent.getContentSize())//cc.winSize

        let xMin = -cW / 2 + winSize.width / 2;
        let xMax = cW / 2 - winSize.width / 2;
        let yMin = -cH / 2 + winSize.height / 2;
        let yMax = cH / 2 - winSize.height / 2;

        x = x > xMax ? xMax : x
        x = x < xMin ? xMin : x;
        y = y > yMax ? yMax : y;
        y = y < yMin ? yMin : y;

        //console.log("移动",x,y);
        // cc.vv.mapPos = cc.v2(x, y);
        // cc.vv.mapScale = this.node.scaleX;
        this.node.setPosition(cc.v2(x, y))
    }

    //是否最大缩放
    isMaxScale(sc) {
        return sc > this.maxScale;
    }
    //是否最小缩放
    isMinScale(sc) {
        // return false;
        return this.getMinScale() > sc;
    }

    //获取最小缩放比
    getMinScale() {
        return this.minScale;
        //console.log(cc.view.getCanvasSize().width,cc.view.getCanvasSize().height)
        //console.log(cc.winSize.width,cc.winSize.height)
        let windowSize = cc.size(this.node.parent.getContentSize())//cc.winSize;
        let mapRect = this.node
        let scaleX = windowSize.width / mapRect.width;
        let scaleY = windowSize.height / mapRect.height;
        let scale = scaleX > scaleY ? scaleX : scaleY;
        //console.log("scale",scale)
        return scale;
    }
}
